package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.CheckItemException;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: dalao
 * @date: 2020/4/5-21:50
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * 前端发送的json类型的数据, 用map或者pojo类接受的时候,参数加@RequestBody注解
     */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem) {
        //try-catch 如果成功向前端发送执行结果,如果抓取到错误会向前端发送执行错误的结果
        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /**
     * 分页查询,使用pageHelper插件
     */
    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        //查询的结果封装到pageResult中
        PageResult pageResult = checkItemService.findPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;

    }

    /**
     * 删除检查项
     */
    @RequestMapping("/deleteById")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result deleteById(@RequestParam("id") Integer id) {
        try {
            checkItemService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (CheckItemException c) {
            return new Result(false, c.getMessage());
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    /**
     * 根据id查询数据,返回数据给编辑页面
     */
    @RequestMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return Result.OK("", checkItem);
        } catch (Exception e) {
            return Result.ERROR(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * 修改检查项数据
     */
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
            return Result.OK(MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            return Result.ERROR(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    /**
     * 查询所有的检查项, 返回给检查组新增界面
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckItem> checkItems = checkItemService.findAll();
            return Result.OK("查询检查项成功",checkItems);
        }catch (Exception e){
            return Result.ERROR("网略异常");
        }
    }
}
